var t = require('../../@babel/runtime/helpers/defineProperty'),
  e = require('../../@babel/runtime/helpers/regeneratorRuntime'),
  a = require('../../@babel/runtime/helpers/asyncToGenerator'),
  s = getApp(),
  o = !1,
  i = null;
Page({
  data: {
    first: [],
    statusBarHeight: s.globalData.statusBarHeight,
    navBarHeight: s.globalData.navBarHeight,
    firstChooseId: 0,
    topOldIndex: 0,
    topNewIndex: 0,
    allList: [],
    _scrollTop: 0,
    reqId: 0,
    keyWord: '',
    searchSort: 0,
    sort: 0,
  },
  onLoad: function (t) {
    t.cat_id && wx.setStorageSync('cat_id', t.cat_id || 0);
  },
  getAllSpe: function (t) {
    var o = this;
    return a(
      e().mark(function a() {
        var i, r, n, d, c, l;
        return e().wrap(function (e) {
          for (;;)
            switch ((e.prev = e.next)) {
              case 0:
                return (e.next = 2), s.$api.goods.speIndexAll2();
              case 2:
                if (
                  ((i = e.sent),
                  (r = i[0].id || 0),
                  (n = 0),
                  (d = o.data),
                  (c = d.firstChooseId),
                  d.reqId,
                  t)
                )
                  for (l in i) i[l].id == c && ((r = c), (n = l));
                o.setData({
                  first: i,
                  topNewIndex: n,
                  allList: i.map(function (t) {
                    return {
                      id: t.id,
                      list: [],
                      second_data: t.second_data,
                      page: 1,
                      sId: '',
                      sort: 0,
                    };
                  }),
                  firstChooseId: t ? r : i[0].id || 0,
                  reqId: t ? r : i[0].id || 0,
                }),
                  o.firstIdChange(t ? r : i[0].id || 0, t);
              case 9:
              case 'end':
                return e.stop();
            }
        }, a);
      }),
    )();
  },
  chooseFirst: function (t) {
    var e = t.currentTarget.dataset,
      a = e.item,
      s = e.index,
      o = a.id;
    o != this.data.firstChooseId &&
      (this.setData({
        firstChooseId: o || 0,
        reqId: o || 0,
        topOldIndex: this.data.topNewIndex,
        topNewIndex: s,
      }),
      this.firstIdChange(o, !1));
  },
  chooseSecond: function (e) {
    var a,
      s = e.currentTarget.dataset.sid,
      o = this.data,
      i = o.allList,
      r = o.topNewIndex;
    this.setData(
      (t((a = {}), 'allList['.concat(r, '].sId'), s == i[r].sId ? '' : s),
      t(a, 'allList['.concat(r, '].page'), 1),
      a),
    ),
      this.secondIdChange();
  },
  chooseSort: function (e) {
    var a = e.currentTarget.dataset,
      s = a.sort;
    if ('search' === a.type) {
      var o,
        i = this.data.allList;
      this.setData(
        (t((o = {}), 'allList['.concat(0, '].sort'), s == i[0].sort ? 0 : s),
        t(o, 'allList['.concat(0, '].page'), 1),
        t(o, 'searchSort', s),
        o),
      ),
        this.searchGoods();
    } else {
      var r,
        n = this.data,
        d = n.allList,
        c = n.topNewIndex;
      this.setData(
        (t((r = {}), 'allList['.concat(c, '].sort'), s == d[c].sort ? 0 : s),
        t(r, 'allList['.concat(c, '].page'), 1),
        r),
      ),
        this.secondIdChange();
    }
  },
  secondIdChange: function () {
    var e = this,
      a = this.data,
      o = a.topNewIndex,
      i = a.allList[o];
    s.$api.goods[i.sId ? 'speList' : 'speIndex']({
      type_id: i.id,
      page: i.page,
      second_cat_id: i.sId,
      sort: i.sort || 0,
    }).then(function (a) {
      var s = e.selectComponent('#list_'.concat(i.id));
      s._refresh(),
        e.setData(t({}, 'allList['.concat(o, '].list'), a.goodsList)),
        0 !== a.goodsList.length ? s._gotNewData(a.goodsList) : s._noMore();
    });
  },
  firstIdChange: function (e, a) {
    var o = this,
      i = this.data,
      r = i.reqId,
      n = i.topNewIndex,
      d = i.allList;
    d[n].list.length ||
      s.$api.goods
        .speIndex({ type_id: a ? r : e, page: 1, sort: d[n].sort })
        .then(function (a) {
          var s = o.selectComponent('#list_'.concat(e)),
            i = d[n];
          (i.page = 2),
            (i.list = a.goodsList),
            o.setData(t({}, 'allList['.concat(n, ']'), i)),
            0 !== a.goodsList.length ? s._gotNewData(a.goodsList) : s._noMore();
        });
  },
  onReady: function () {},
  onShow: function () {
    var t = wx.getStorageSync('search_word');
    this.setData({ search_word: t }),
      'function' == typeof this.getTabBar &&
        this.getTabBar() &&
        this.getTabBar().setData({ selected: 1 }),
      this.selectComponent('#IndexNav')._getMessageNum();
    var e = wx.getStorageSync('cat_keyWord'),
      a = wx.getStorageSync('cat_id'),
      s = this.data.first;
    e
      ? (this.setData({
          keyWord: e,
          first: [],
          firstChooseId: 0,
          topOldIndex: 0,
          topNewIndex: 0,
          allList: [],
          reqId: 0,
        }),
        this.searchGoods())
      : '' !== a
        ? (this.setData({
            first: [],
            firstChooseId: a,
            topOldIndex: 0,
            topNewIndex: 0,
            allList: [],
            reqId: a || 0,
          }),
          this.getAllSpe(!!a))
        : 0 == s.length && this.getAllSpe(!1);
  },
  searchGoods: function () {
    var e = this,
      a = this.data,
      o = a.keyWord,
      i = (a.allList, a.searchSort);
    s.$api.goods
      .speList({ keywords: o, page: 1, ac_type: '', sort: i })
      .then(function (a) {
        var s = e.selectComponent('#list_search');
        s._refresh();
        var o = { id: 'search', list: a.goodsList, page: 2 };
        e.setData(t({}, 'allList['.concat(0, ']'), o)),
          0 !== a.goodsList.length ? s._gotNewData(a.goodsList) : s._noMore();
      });
  },
  reloadData: function () {
    var t = this;
    return a(
      e().mark(function a() {
        return e().wrap(function (e) {
          for (;;)
            switch ((e.prev = e.next)) {
              case 0:
                t.setData({
                  first: [],
                  firstChooseId: -1,
                  topOldIndex: 0,
                  topNewIndex: 0,
                  allList: [],
                });
              case 1:
              case 'end':
                return e.stop();
            }
        }, a);
      }),
    )();
  },
  onHide: function () {
    clearTimeout(i),
      wx.removeStorageSync('cat_keyWord'),
      wx.removeStorageSync('cat_id'),
      this.setData({ keyWord: '' });
  },
  onUnload: function () {
    clearTimeout(i),
      wx.removeStorageSync('cat_keyWord'),
      wx.removeStorageSync('cat_id');
  },
  onPullDownRefresh: function () {},
  getThisMore: function () {
    var e = this;
    if (!o) {
      o = !0;
      var a = this.data,
        i = a.allList,
        r = a.topNewIndex,
        n = a.firstChooseId,
        d = a.keyWord;
      s.$api.goods[d || i[r].sId ? 'speList' : 'speIndex']({
        type_id: d ? 0 : n,
        keywords: d || '',
        ac_type: '',
        sort: i[r].sort || 0,
        second_cat_id: i[r].sId,
        page: i[r].page,
      }).then(function (a) {
        o = !1;
        var s = e.selectComponent('#list_'.concat(d ? 'search' : n));
        0 !== a.goodsList.length
          ? (s._gotNewData(a.goodsList),
            e.setData(t({}, 'allList['.concat(r, '].page'), i[r].page + 1)))
          : s._noMore();
      });
    }
  },
  onReachBottom: function () {
    console.log('bottom '), this.getThisMore();
  },
  onPageScroll: function (t) {
    var e = this;
    clearTimeout(i),
      (i = setTimeout(function () {
        e.setTabbar_top(t.scrollTop), e.setData({ _scrollTop: t.scrollTop });
      }, 10));
  },
  setTabbar_top: function (t) {
    var e = this.data.__tabbar_show_top;
    t > 400 && !e
      ? ((this.data.__tabbar_show_top = !0),
        'function' == typeof this.getTabBar &&
          this.getTabBar() &&
          this.getTabBar().setData({ show_back_top: !0 }))
      : t <= 400 &&
        e &&
        ((this.data.__tabbar_show_top = !1),
        'function' == typeof this.getTabBar &&
          this.getTabBar() &&
          this.getTabBar().setData({ show_back_top: !1 }));
  },
  onShareAppMessage: function () {},
});
